import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/userLogin.vue'
import Home from '../components/userHome.vue'
import Welcome from '../components/userWelcome.vue'
import Users from '../components/user/userUsers.vue'
import Rights from '../components/power/userRights.vue'
import Roles from '../components/power/userRoles.vue'
import Cate from '../components/goods/goodsCate.vue'
import Params from '../components/goods/goodsParams.vue'
import GoodsList from '../components/goods/goodsList.vue'
import Add from '../components/goods/goodsAdd.vue'
import Order from '../components/order/orderOrder.vue'
import Report from '../components/report/reportReport.vue'
Vue.use(VueRouter)

const routes = [
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes:[
    {
      path:'/login',component:Login
    },{
      path:'/',redirect:'/login'
    },
    {
      path:'/home',component:Home,
      redirect:'/welcome',
      children:[
        {
          path:'/welcome',component:Welcome
        },{
          path:'/users',component:Users
        },{
          path:'/rights',component:Rights
        },
        {
          path:'/roles',component:Roles
        },
        {
          path:'/categories',component:Cate
        },
        {
          path:'/params',component:Params
        },
        {
          path:'/goods',component:GoodsList
        },
        {
          path:'/goods/add',component:Add
        },
        {
          path:'/orders',component:Order
        },
        {
          path:'/reports',component:Report
        }
      ]
    },
  ]
})
  //挂载路由导航守卫
  router.beforeEach ( (to, from,next) => { 
  // to 将要访问的路径
  // from 代表是从哪个路径跳转而来
  // next是一个函数 表示放行
  // next（）放行 next（‘login’）强制跳转
  //如果是login直接跳转
  if(to.path === '/login') return next()
  // 从sessionstorage 获取token值
  const tokenStr = window.sessionStorage.getItem('token')
    // 没有token 强制跳转登录页
  if(!tokenStr) return next('/login')
    next()
}
  )

export default router
